package com.jydd.singlekotlinmvvm.comm.base

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean

/**
 *  author: 韦敏敏
 *  date: 2020/1/21 0021
 */
class SingleLiveEvent<T : Any?> : MutableLiveData<T?>() {
    private val mPending = AtomicBoolean(false)

    override fun observe(owner: LifecycleOwner, observer: Observer<in T?>) {
        if (hasActiveObservers()){
            Timber.w("Multiple observers registered but only one will be notified of changes.")
        }
        super.observe(owner, Observer {
            if (mPending.compareAndSet(true, false)){
                observer.onChanged(it)
            }
        })
    }

    override fun setValue(value: T?) {
        mPending.set(true)
        super.setValue(value)
    }
    fun call() {
        value = null
    }

}